<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Patient Information</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            text-align: center;
        }
        .container {
            display: flex;
            align-items: center;
        }
        .left {
            flex: 1;
            padding: 20px;
        }
        .left img {
            max-width: 100%;
            height: auto;
        }
        .right {
            flex: 2;
            padding: 20px;
        }
        .patient-info {
            margin-bottom: 20px;
        }
        .edit-buttons {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .edit-button {
            margin-right: 2px;
            padding: 10px 100px;
            font-size: 16px;
        }
        .upload-button {
            margin-top: 10px;
            padding: 10px 100px;
            font-size: 16px;
            background-color: #007bff;
            color: #fff;
            border: none;
            cursor: pointer;
        }
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 10px;
        }
    </style>
</head>
<body>
    <h1>医学影像PACS</h1>
    <div class="container">
        <div class="left">
            <img id="patient-image" src="/static/Decrypted_patient4.jpg" alt="Patient Image">
        </div>
        <div class="right">
            <div class="patient-info">
                <h2>患者信息</h2>
                <table>
                    <tr>
                        <th>项目</th>
                        <th>信息</th>
                    </tr>
                    <tr>
                        <td>姓名:</td>
                        <td><span id="patient-name">赵六</span></td>
                    </tr>
                    <tr>
                        <td>性别:</td>
                        <td><span id="patient-gender">女</span></td>
                    </tr>
                    <tr>
                        <td>年龄:</td>
                        <td><span id="patient-age">50</span></td>
                    </tr>
                    <tr>
                        <td>病况:</td>
                        <td><span id="patient-condition">脑卒中恢复期，左侧肢体无力，言语困难，需进行长期康复治疗</span></td>
                    </tr>
                </table>
                <div id="upload-message" style="margin-top: 10px;"></div>
            </div>
            <div class="edit-buttons">
                <button class="edit-button" id="edit-button" onclick="enableEdit()">修改患者信息</button>
                <button class="edit-button" id="save-button" onclick="saveChanges()" style="display: none;">保存患者信息</button>
                <button class="edit-button" id="delete-button" onclick="deletePatient()">删除患者信息</button>
                <button class="edit-button" id="encrypt-button" onclick="showEncryptDialog()">加密患者影像</button>
            </div>
        </div>
    </div>

    <div id="encrypt-dialog" style="display: none;">
        <input type="text" id="encryption-key" placeholder="输入加密密钥"><br>
        <input type="number" id="encryption-rounds" placeholder="输入加密轮数"><br>
        <button onclick="encryptPatientImage()">确定</button>
    </div>

    <script>
        function enableEdit() {
            document.getElementById("patient-name").contentEditable = true;
            document.getElementById("patient-gender").contentEditable = true;
            document.getElementById("patient-age").contentEditable = true;
            document.getElementById("patient-condition").contentEditable = true;
            document.getElementById("edit-button").style.display = "none";
            document.getElementById("save-button").style.display = "block";
        }

        function saveChanges() {
            document.getElementById("patient-name").contentEditable = false;
            document.getElementById("patient-gender").contentEditable = false;
            document.getElementById("patient-age").contentEditable = false;
            document.getElementById("patient-condition").contentEditable = false;
            document.getElementById("edit-button").style.display = "block";
            document.getElementById("save-button").style.display = "none";
        }

        function deletePatient() {
            document.getElementById("patient-name").textContent = "";
            document.getElementById("patient-gender").textContent = "";
            document.getElementById("patient-age").textContent = "";
            document.getElementById("patient-condition").textContent = "";
            document.getElementById("edit-button").style.display = "block";
            document.getElementById("save-button").style.display = "none";
        }

        function showEncryptDialog() {
            document.getElementById("encrypt-dialog").style.display = "block";
        }

        function hideEncryptDialog() {
            document.getElementById("encrypt-dialog").style.display = "none";
        }

        function encryptPatientImage() {
            const key = document.getElementById("encryption-key").value;
            const rounds = document.getElementById("encryption-rounds").value;

            // 创建FormData对象
            const formData = new FormData();
            formData.append('key', key);
            formData.append('rounds', rounds);

            // 发送POST请求到后端处理加密操作
            fetch('/encrypt_patient4', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                // 将加密后的图像显示在页面上
                const encryptedImage = data.encrypted_image;
                const imageUrl = 'data:image/jpeg;base64,' + encryptedImage;
                document.getElementById("patient-image").src = imageUrl;

                // 显示加密后的轮数
                const encryptedRounds = data.encrypted_rounds;
                alert("加密后的轮数: " + encryptedRounds);

                // 将患者信息变成星号
                document.getElementById("patient-name").textContent = "****";
                document.getElementById("patient-gender").textContent = "**";
                document.getElementById("patient-age").textContent = "**";
                document.getElementById("patient-condition").textContent = "********";
            })
            .catch(error => {
                console.error('Error:', error);
            });

            hideEncryptDialog();
        }

        // 页面加载时显示原始未加密的图片
        window.onload = function() {
            const originalImageUrl = '/static/Patient4.jpg';  // 未加密图片的URL
            document.getElementById("patient-image").src = originalImageUrl;
        };
    </script>
</body>
</html>
